package com.example.exp03

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.exp03.ui.theme.Exp03Theme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            Exp03Theme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    ImageGalleryApp(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }
}

data class Artwork(
    val imageResId: Int,
    val title: String,
    val artist: String,
    val year: Int
)

@Composable
fun ImageGalleryApp(modifier: Modifier = Modifier) {
    val artworks = listOf(
        Artwork(R.drawable.image02, "篮球赛1", "张三", 2023),
        Artwork(R.drawable.image03, "篮球赛2", "李四", 2024)
    )
    
    var currentIndex by remember { mutableStateOf(0) }
    val currentArtwork = artworks[currentIndex]
    
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(20.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 图片显示区域
        ImageDisplayArea(
            painter = painterResource(id = currentArtwork.imageResId),
            modifier = Modifier
                .fillMaxWidth()
                .height(380.dp)
                .padding(bottom = 20.dp)
        )
        
        // 信息区域
        ArtworkInfo(
            title = currentArtwork.title,
            artist = currentArtwork.artist,
            year = currentArtwork.year,
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 20.dp)
        )
        
        // 导航区域
        NavigationButtons(
            onPreviousClick = {
                currentIndex = if (currentIndex > 0) currentIndex - 1 else artworks.size - 1
            },
            onNextClick = {
                currentIndex = if (currentIndex < artworks.size - 1) currentIndex + 1 else 0
            },
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 20.dp)
        )
    }
}

@Composable
fun ImageDisplayArea(
    painter: Painter,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Image(
            painter = painter,
            contentDescription = "展示图片",
            modifier = Modifier
                .size(280.dp, 210.dp)
        )
    }
}

@Composable
fun ArtworkInfo(
    title: String,
    artist: String,
    year: Int,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.Start
    ) {
        Text(
            text = "标题: $title",
            fontSize = 16.sp,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 8.dp)
        )
        Text(
            text = "艺术家: $artist ($year)",
            fontSize = 14.sp
        )
    }
}

@Composable
fun NavigationButtons(
    onPreviousClick: () -> Unit,
    onNextClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Button(
            onClick = onPreviousClick,
            modifier = Modifier
                .weight(1f)
                .padding(horizontal = 20.dp)
                .height(40.dp)
        ) {
            Text("PREV")
        }
        
        Spacer(modifier = Modifier.width(20.dp))
        
        Button(
            onClick = onNextClick,
            modifier = Modifier
                .weight(1f)
                .padding(horizontal = 20.dp)
                .height(40.dp)
        ) {
            Text("NEXT")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun ImageGalleryPreview() {
    Exp03Theme {
        ImageGalleryApp()
    }
}